package cn.nexd.map.algorithm;


/**
 * Created by admin on 15/8/19.
 */
public class LocationAlgorithm {
    public Location_Result locateCoordinate(Double[] input, NB_Classifier clf){
        return _locateCoordinate(input, clf);
    }

    private Location_Result _locateCoordinate(Double[] input, NB_Classifier clf){
        System.out.println("Algorithm Started\n======================================");
        return _getResult(input, clf);
    }

    private Location_Result _getResult(Double[] input, NB_Classifier clf){
        Double[] res = _blockProbability(input, clf);
        int index = _argmax(res);
        Double[] pos = clf.res_pos[index];
        return new Location_Result("",Double.toString(pos[0]),Double.toString(pos[1]));
    }

    private int _argmax(Double[] list){
        int ind = 0;
        double max_v = list[0];
        for(int i = 0; i < list.length; i++){
            if(list[i]>max_v){
                ind = i;
                max_v = list[i];
            }
        }
        return ind;
    }

    private Double[] _blockProbability(Double[] input, NB_Classifier clf){
        Double[] res = new Double[clf.res_pos.length];
        for(int i = 0;i<res.length;i++){
            res[i] = _calProbability(input, clf.post_mean[i], clf.post_var[i]);
//            Log.d("onLocation", "Result " + i + " is " + res[i]);
        }
        return res;
    }

    private double _calProbability(Double[] input, Double[] mean, Double[] var){
        double res = 0.0;
        for(int i = 0; i < input.length; i++){
            //res*= 1/math.Sqrt(sigma[i]) * math.Exp(-math.Pow(rss[i] - mean[i], 2)/sigma[i])
            //This Part Should Be modified to get proper value instead of just multiply the result.
            if(input[i] == 0){
                res+= 0;
            }else {
                double temp = 1.0 / Math.sqrt(var[i]) * Math.exp(-Math.pow(input[i] + 100.0 - mean[i], 2) / var[i]) * (input[i] + 100.0) / 10.0;
                res += temp;
//                System.out.printf("Input %d, Value %f, Mean %f, Var %f, Prob %f\n", i, input[i], mean[i], var[i], temp);
            }
        }
//        System.out.println(res);
        return res;
    }
}
